/*******************************************************************************************************

Figure 1: Petitions sent between 1820 and 1838.


*******************************************************************************************************/


*Set directory path
cd "C:\Users\tsa23\Documents\petitions\replication files"

clear all

*read in our main dataset
use dta\did_rep.dta
drop if ID == 45 //University of Cambridge
drop if ID == 191 //University of Oxford

*code dummy variables that indentify the two treatment and control groups 
gen C_G = 0
replace C_G  = 1 if drummond ==. & new == 0
gen C_L = 0 
replace C_L = 1 if drummond == 0 | (drummond == 1 & sch_B ==1)
gen T_G = new
gen T_L = sch_A

*allocate petitions to the four groups
foreach i in "T_G" "C_G" "T_L" "C_L" { 
gen `i'_pet = 0
replace `i'_pet = petitions if `i'==1
}

*Sum petitions for the two treatment and control groups by year
collapse (sum) T_G_pet (sum) C_G_pet  (sum) T_L_pet (sum) C_L_pet, by(year)

*drop the reform years
drop if year==1830 |  year==1831 | year==1832 

*create a treatment dummy
gen treatperiod = 0
replace treatperiod = 1 if year>1832  
 
*create dataset with average for before and after for the four groups
collapse (mean) T_G_pet (mean) C_G_pet  (mean) T_L_pet (mean) C_L_pet, by(treatperiod)

*Deine variables with average petitions per constituency in the two treatment and control groups
gen T_G_B = T_G/42
label var T_G_B "Petitions from Case G treatment group per borough"

gen C_G_B = C_G/93
label var C_G_B "Petitions from Case G control group per borough"

gen T_L_B = T_L/56
label var T_L_B "Petitions from Case L treatment group"

gen C_L_B = C_L/64
label var C_L_B "Petitions from Case L control group per borough"

keep treatperiod T_L_B C_L_B T_G_B C_G_B


*Make names reshape-friendly (drop trailing "_B")
capture rename T_G_B T_G
capture rename C_G_B C_G
capture rename T_L_B T_L
capture rename C_L_B C_L

*Reshape over CASE: G/L
reshape long T_ C_, i(treatperiod) j(case) string
    // produces vars: T_ C_ and string j var "case" = "G" or "L"

rename T_ T
rename C_ C

*Stack T vs C into rows (treattype dimension)
rename T valueT
rename C valueC
reshape long value, i(treatperiod case) j(treattype) string
    // treattype = "T" or "C"; value = the number

*Pretty labels / final variable names
replace case = "Case " + case
replace treattype = cond(treattype=="T","Treatment","Control")
rename value petion_const

*Period label as text
gen str9 treatperiod1 = cond(treatperiod==0,"1820-29","1833-38")

order treatperiod case treattype petion_const treatperiod1
sort  treatperiod case treattype
list, noobs

*produce the histograms


set scheme plotplainblind

label var treatperiod "Indicator for after period (1833-38)"
label var treatperiod1 "String for before and after period"
label var petion_const "Petitions per year per borough"
label var case "Treatment G or L"
label var treattype "String for control or treatment"


graph hbar  petion_const if case == "Case G",   over(treattype,  label(labsize(small) )) over(treatperiod1, sort(1)   label(labsize(small)))  blabel(bar, format(%2.1f) angle(90)) ytitle("Petitions per year per constituency")  asyvars intensity(25) bargap(10)  bar(1, lpattern(solid) color(grey) lwidth(0.25)   ) bar(2, lpattern(dash) lwidth(0.25))  bar(3, lpattern(dot) lwidth(0.25)) bar(4, lpattern(dash_dot) lwidth(0.25)) bar(5, lpattern(longdash) lwidth(0.25)) bar(6, lpattern(shortdash) lwidth(0.25) ) bar(7, lpattern(shortdash) lwidth(0.25) ) bar(8, lpattern(shortdash) lwidth(0.25) )
graph export graphs\figure1a.pdf, as(pdf) name("Graph") replace


graph hbar  petion_const if case == "Case L",   over(treattype,  label(labsize(small) )) over(treatperiod1, sort(1)   label(labsize(small)))  blabel(bar, format(%2.1f) angle(90)) ytitle("Petitions per year per constituency")  asyvars intensity(25) bargap(10)  bar(1, lpattern(solid) color(grey) lwidth(0.25)   ) bar(2, lpattern(dash) lwidth(0.25))  bar(3, lpattern(dot) lwidth(0.25)) bar(4, lpattern(dash_dot) lwidth(0.25)) bar(5, lpattern(longdash) lwidth(0.25)) bar(6, lpattern(shortdash) lwidth(0.25) ) bar(7, lpattern(shortdash) lwidth(0.25) ) bar(8, lpattern(shortdash) lwidth(0.25) )
graph export graphs\figure1b.pdf, as(pdf) name("Graph") replace 
 